#ifndef LST_TIMER
#define LST_TIMER

#include <iostream>
#include <functional>
#include <netinet/in.h>
#include <time.h>

const int BUFFER_SIZE = 64;
using std::cout;
using std::endl;

class utilTimer;
struct clientData {
    sockaddr_in address;
    int sockFd;
    char buf[BUFFER_SIZE];
    utilTimer* timer;
};

struct utilTimer {
    using cb_func = std::function<void(clientData*)>;
    time_t expire;
    cb_func cb;
    clientData* userData;
    utilTimer* prev;
    utilTimer* next;

    utilTimer() : prev(nullptr), next(nullptr) {}
};

class sortTimerLst {
private:
    utilTimer* head;
    utilTimer* tail;

public:
    sortTimerLst() : head(nullptr), tail(nullptr) {}
    ~sortTimerLst() {
        utilTimer* cur = head;
        // 遍历清理链表中的资源
        while (cur) {
            utilTimer* next = cur->next;
            delete cur;
            cur = next;
        }
    }
    // 对外接口函数 添加、更新、删除、运行
    void addTimer (utilTimer* timer) {
        if (!timer) {
            return;
        }
        if(!head) {
            head = tail = timer;
        }
        if(timer->expire < head->expire) {
            timer->next = head;
            head->prev = timer;
            head = timer;
            return;
        }
        addTimer (timer, head);
    }

    void adjustTimer (utilTimer* timer) {
        if(!timer) {
            return;
        }
        utilTimer* next = timer->next;
        // timer在尾部或者调整后的仍然小于下一个
        if (!next || timer->expire < next->expire) {
            return;
        }
        // timer在头部，直接取出并重新置入链表中
        if (timer == head) {
            head = head->next;
            head->prev = nullptr;
            timer->next = nullptr;
            addTimer(timer, head);
        }
        // timer处于中间，则取出并置入到后面
        else {
            timer->prev->next = timer->next;
            timer->next ->prev = timer->prev;
            addTimer(timer, timer->next);
        }
    }

    void delTimer(utilTimer* timer) {
        if (!timer) {
            return;
        }
        if (timer == head && timer == tail) {
            delete timer;
            head = tail = nullptr;
            return;
        }
        if (timer == head) {
            head = head->next;
            head->prev = nullptr;
            delete timer;
            return;
        }
        if (timer == tail) {
            tail = tail->prev;
            tail->next = nullptr;
            delete timer;
            return;
        }
        timer->prev->next = timer->next;
        timer->next->prev = timer->prev;
        delete timer;
    }

    void tick () {
        if (!head) {
            return;
        }
        cout << "timer tick" << endl;
        time_t curTime = time(nullptr); // 获取当前时间
        utilTimer* cur = head;
        // 遍历当前链表， 将小于当前时间的都删除
        while (cur) {
            if (curTime < cur->expire) {
                break;
            }
            cur->cb(cur->userData);
            head = cur->next;
            if (head) {
                head->prev = nullptr;
            }
            delete cur;
            cur = head;
        }
    }

private:
    void addTimer(utilTimer* timer, utilTimer* lstHead) {
        utilTimer* prev = lstHead;
        utilTimer* cur = prev->next;
        while (cur) {
            // prev - > timer - > cur
            if (timer->expire < cur->expire) {
                prev->next = timer;
                timer->next = cur;
                cur->prev = timer;
                timer->prev = prev;
                break;
            }
            prev = cur;
            cur = cur->next;
        }
        // 遍历完都没有插入进去 说明只能插入到最后了
        if (!cur) {
            prev->next = timer;
            timer->next = nullptr;
            timer->prev = prev;
            tail = timer;
        }
    }
};

#endif